//
//  TicketVc.m
//  Shengzhuli
//
//  Created by FXGuo on 2018/1/17.
//  Copyright © 2018年 kamangkeji. All rights reserved.
//

#import "TicketVc.h"
#import "TicketViewCell.h"
#import "TicketCodeVc.h"
#import "NewHomeNetwork.h"
#import <MJRefresh.h>
#import "SZLAccountHelper.h"
#import "MBProgressHUD+MJ.h"

@interface TicketVc ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong) UITableView *tableView;
@property (nonatomic,assign) NSInteger page;
@property (nonatomic,strong) NSMutableArray *tickets;

@end

@implementation TicketVc

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"优惠券";
    self.page = 1;
    [self.view addSubview:self.tableView];
    [self.tableView.mj_header beginRefreshing];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tickets.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TicketViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([TicketViewCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.tickets[indexPath.row];
    return cell;
        
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TicketCodeVc *vc = [[TicketCodeVc alloc]init];
    vc.model = self.tickets[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)getTicket
{
    NSMutableDictionary *paremes = [NSMutableDictionary dictionary];
    paremes[@"token"] = [SZLAccountHelper shareInstance].token;
    paremes[@"pageNo"] = @(self.page);
    [NewHomeNetwork getTicketListWithParame:paremes response:^(BOOL success, NSString *msg, NSArray *lists) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self hideNoDataLab];
        if (success) {
            self.tableView.mj_footer.hidden = (lists.count < 20);
            if (self.page == 1 && lists.count == 0) {
                [self showNoDataLab:self.tableView range:CGRectMake(0, self.tableView.height*0.5-30, ScreenW, 20) title:@"暂无数据"];
                return ;
            }
            [self.tickets addObjectsFromArray:lists];
            [self.tableView reloadData];
        }else {
            [MBProgressHUD showError:msg];
            self.tableView.mj_footer.hidden = YES;
        }
    }];
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-SafeAreaTopHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = RGBColor(239, 239, 244);
        _tableView.tableFooterView = [[UIView alloc]init];
        _tableView.rowHeight = 120;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([TicketViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([TicketViewCell class])];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.page = 1;
            [self.tickets removeAllObjects];
            [self getTicket];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            self.page++;
            [self getTicket];
        }];
    }
    return _tableView;
}
- (NSMutableArray *)tickets
{
    if (!_tickets) {
        _tickets = [NSMutableArray array];
    }
    return _tickets;
}

@end
